<?php

class DansController extends BaseController {

	/*
	|--------------------------------------------------------------------------
	| 登陆控制器
	|--------------------------------------------------------------------------
	|
	| 登陆、验证、登出  功能
	|
	*/

	//登陆
	public function index()
	{
		if(Auth::check()){
			return Redirect::route('home');
		}else{
			return View::make('login.index');
		}
	}

	// 验证
	public function auth(){
		$input = Input::all();
		$rule = array(
			'username' => 'required|min:3',
			'password' => 'required|between:3,64',
			);
		$message = array(
			'required' => ':attribute 是必填项',
			'username.min'=>':attribute 应大于 :min 个字符',
			'between'	=> ':attribute 应在 :min - :max 个字符之间'
			);
		$validator = Validator::make($input, $rule, $message);
		if($validator->fails()){
			return Redirect::route('login.index')
			->withErrors($validator)->withInput();
		}else{
			$user = User::where('username',$input['username'])->first();
			if($user != null){
				if (Auth::attempt(array('username' => $input['username'], 'password' => $input['password']))){
					// 存点东西到session
					Session::put('username', $user->username);
					Session::put('email', $user->email);
					Session::put('permission', $user->permission);
					Session::put('updated_at', $user->updated_at);
					Session::put('nicename', $user->nicename);
					Session::put('identity', $user->identity);
					Session::put('avatar',$user->avatar);

					// 存点东西到cookie
					


				    return Redirect::route('home');
				}else{
					return Redirect::route('login.index')->withErrors('密码错误')->withInput();
				}
			}else{
				return Redirect::route('login.index')->withErrors('没有该用户')->withInput();
			}
		}
	}

	//登出 
	public function logout(){
		Auth::logout();
		return Redirect::route('login.index');
	}

}
